import * as WeChat from 'react-native-wechat'
import {pay} from 'react-native-alipay'
import {requestData} from './NetUtil'
import Constant from '../Contant'
import NetRequest from '../util/NetRequest'

export default class PayUtil {
    //微信支付
    static async wechatPay(wexinSpec, prepayId, callback) {
        try {
            let wx = {
                appId: wexinSpec.appid,
                nonceStr: wexinSpec.noncestr,
                package: wexinSpec.packageValue,
                partnerId: wexinSpec.partnerid,
                sign: wexinSpec.sign,
                timeStamp: wexinSpec.timestamp,
                prepayId: prepayId
            }
            const result = await WeChat.pay(wx);
            callback(result)
        }
        catch (e) {
            alert(e)
        }
    }

    // 支付宝
    static async alipay(param) {
        return await pay(param, true);
    }

    static async pay(json, callback) {
        let channel = json.channel
        let wexinSpec = json.wexinSpec
        let orderInfo = json.paras

        let prepayId = json.tn
        let payType = channel == 4 ? Constant.AlipayClient : Constant.WeixinPay;
        switch (payType) {
            case Constant.WeixinPay: {
                this.wechatPay(wexinSpec, prepayId, callback);
                break;
            }
            case Constant.AlipayClient: {
                // 支付宝
                const result = await pay(orderInfo.info, true);
                callback(result);
                break;
            }
        }
    }

    static async confirm(openId, orderId, payChannel, callback) {
        let param = {
            "openId": openId,
            "orderId": orderId,
            "payChannel": payChannel
        };
        NetRequest.confirmOrderPay(param).then((result)=>{
            callback(result);
        });
    }


    // 支付结果查询 【微信 支付宝】
    static async queryPay(payInfo){
        let param = {
            "createTime": "",
            "description": "",
            "openId": "",
            "orderNo": payInfo.orderId,
            "payChannel": payInfo.channel,
            "totalFee": 0
        }
        return await  NetRequest.payQueryResult(param)
    }
}


